package com.xq.chrome.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateFormatUtil {
  /**
   * 字符串转为日期格式
   *
   * @param dateString
   * @return
   * @throws ParseException
   */
  public static Date formatStr2Date(String dateString) throws ParseException {
    SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = bartDateFormat.parse(dateString);
    return date;
  }

  /**
   * 字符串转为日期格式
   *
   * @param dateString
   * @return
   * @throws ParseException
   */
  public static Date formatStr2Datetime(String dateString) throws ParseException {
    SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = bartDateFormat.parse(dateString);
    return date;
  }

  /**
   * 字符串转为日期格式
   *
   * @param dateString
   * @return
   * @throws ParseException
   */
  public static Date formatStr2DateTime2(String dateString) throws ParseException {
    SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    Date date = bartDateFormat.parse(dateString);
    return date;
  }

  /**
   * 将时间格式化为含时分秒的字符串
   *
   * @param date
   * @return
   * @throws ParseException
   */
  public static String formatDateTime2Str(Date date) throws ParseException {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return dateFormat.format(date);
  }

  /**
   * 将时间格式化为不含时分秒的字符串
   *
   * @param date
   * @return
   * @throws ParseException
   */
  public static String formatDate2Str(Date date) throws ParseException {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    return dateFormat.format(date);
  }

  public final static Date[] strToDateArray(String dateStr) {
    java.sql.Date date_temp = java.sql.Date.valueOf(dateStr);
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date_temp);
    calendar.add(Calendar.HOUR_OF_DAY, 0);
    calendar.add(Calendar.MINUTE, 0);
    calendar.add(Calendar.SECOND, 0);
    Date beginDate = calendar.getTime();
    calendar.add(Calendar.HOUR_OF_DAY, 23);
    calendar.add(Calendar.MINUTE, 59);
    calendar.add(Calendar.SECOND, 59);
    Date endDate = calendar.getTime();
    return new Date[]{beginDate, endDate};
  }

  /**
   * 通过实践搜索,字符串转日期
   *
   * @param beginDateStr 开始时间
   * @param endDateStr   结束时间
   * @return
   */
  public static Date[] strToDateArray(String beginDateStr, String endDateStr) {
    try {
      if ("".equals(endDateStr) || endDateStr.length() == 0) {
        endDateStr = beginDateStr;
      }
      Date beginDate = formatStr2Date(beginDateStr);
      Date endDate = formatStr2Date(endDateStr);
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(beginDate);
      calendar.set(Calendar.HOUR_OF_DAY, 0);
      calendar.set(Calendar.MINUTE, 0);
      calendar.set(Calendar.SECOND, 0);
      beginDate = calendar.getTime();
      calendar.setTime(endDate);
      calendar.set(Calendar.HOUR_OF_DAY, calendar.getMaximum(Calendar.HOUR_OF_DAY));
      calendar.set(Calendar.MINUTE, 59);
      calendar.set(Calendar.SECOND, 59);
      endDate = calendar.getTime();
      return new Date[]{beginDate, endDate};
    } catch (Exception e) {
      throw new RuntimeException("时间格式化错误");
    }
  }

  public static Date getTheDayOfEndTime(String dateString) throws ParseException {
    Date date = null;
    if (dateString.contains(" ")) {
      date = formatStr2Datetime(dateString);
    } else {
      date = formatStr2Datetime(dateString);
    }
    return getTheDayOfEndTime(date);
  }

  public static Date getTheDayOfEndTime(Date date) throws ParseException {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 23);
    cal.set(Calendar.MINUTE, 59);
    cal.set(Calendar.SECOND, 59);
    return cal.getTime();
  }

  public static Date getTheDayOfStartTime(String dateString) throws ParseException {
    Date date = null;
    if (dateString.contains(" ")) {
      date = formatStr2Datetime(dateString);
    } else {
      date = formatStr2Date(dateString);
    }
    return getTheDayOfStartTime(date);
  }

  public static Date getTheDayOfStartTime(Date date) throws ParseException {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    return cal.getTime();
  }

  /**
   * 获取两天之间相差的天数
   *
   * @param startDate 起始时间
   * @param endDate   结束时间
   * @return
   */
  public static int getBetweenDays(Date startDate, Date endDate) {
    long value = endDate.getTime() - startDate.getTime();
    int i = Long.valueOf(value / (24 * 60 * 60 * 1000)).intValue();
    return i;
  }
}
